#include "udp_server.h"
#include <pthread.h>
#include <stdlib.h>

using namespace std;

void usage(std::string proc)
{
	std::cout << "Usage : " << proc << "port" << std::endl;
}

void *recv_message(void* arg)
{
	udp_server *sp = (udp_server*)arg;
	std::string message;
	while(1) {
		sp->recv_data(message);
		std::cout << "Debug : " << message << std::endl;
	}
}

void *send_message(void* arg)
{
	udp_server *sp = (udp_server*)arg;
	while(1) {
		sp->broadcast();
		sleep(1);
	}
}

// ./chatSystem 8080
int main(int argc, char* argv[])
{
	if(argc != 2) {
		usage(argv[0]);
		return 1;
	}

	udp_server ser(atoi(argv[1]));	// 创建一个服务器对象
	ser.init_server();

	// 一个线程收数据，一个线程发数据
	pthread_t r, s;
	pthread_create(&r, NULL, recv_message, (void*)&ser);
	pthread_create(&r, NULL, send_message, (void*)&ser);

	pthread_join(r, NULL);
	pthread_join(s, NULL);
}
